package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.Organization;
import com.xmy.cultivate.entity.StudentFaceMany;
import com.xmy.cultivate.entity.StudentFaceManyDetail;
import com.xmy.cultivate.mapper.StudentFaceManyMapper;
import com.xmy.cultivate.respons.ResFaceData;
import com.xmy.cultivate.service.IOrganizationService;
import com.xmy.cultivate.service.IStudentFaceDataService;
import com.xmy.cultivate.service.IStudentFaceManyDetailService;
import com.xmy.cultivate.service.IStudentFaceManyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 人脸采集多几帐号 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-04-18
 */
@Service
public class StudentFaceManyServiceImpl extends ServiceImpl<StudentFaceManyMapper, StudentFaceMany> implements IStudentFaceManyService {


    @Autowired
    IStudentFaceDataService iStudentFaceDataService;

    @Autowired
    IStudentFaceManyDetailService iStudentFaceManyDetailService;

    @Autowired
    IStudentFaceManyService iStudentFaceManyService;

    @Autowired
    IOrganizationService iOrganizationService;

    /**
     * 添加多个图片
     * @return
     */
    @Override
    public boolean addFaceDataMany(StudentFaceManyDetail studentFaceManyDetail) throws Exception {
        /*QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>();
        organizationQueryWrapper.eq("type",1);
        organizationQueryWrapper.eq("status",1);
        organizationQueryWrapper.groupBy("ali_account");
        List<Organization> organizationList = iOrganizationService.list(organizationQueryWrapper);*/
        StudentFaceManyDetail studentFaceManyDetailInfo = iStudentFaceManyDetailService.getById(studentFaceManyDetail.getId());
        if (studentFaceManyDetailInfo.getPushMqType().equals(2)){
            //已经被处理过了
            return true;
        }
        QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>();
        organizationQueryWrapper.eq("type",1);
        organizationQueryWrapper.eq("status",1);
        organizationQueryWrapper.eq("ali_account",studentFaceManyDetail.getAliAccount());
        organizationQueryWrapper.last("limit 1");
        Organization organization = iOrganizationService.getOne(organizationQueryWrapper);
        if (organization == null){
            studentFaceManyDetail.setPushMqType(3);//异常
            studentFaceManyDetail.setRemark("人脸帐号校区不存在");
            iStudentFaceManyDetailService.updateById(studentFaceManyDetail);
            return false;
        }
        ResFaceData resFaceData = iStudentFaceDataService.setDataForMany(organization.getId(),studentFaceManyDetail,studentFaceManyDetailInfo.getFaceType());
        if (resFaceData.getCode().equals(Code.OK)){
            studentFaceManyDetail.setPushMqType(2);
            studentFaceManyDetail.setEntityId(resFaceData.getEntityId());
            studentFaceManyDetail.setFaceId(resFaceData.getFaceId());
            studentFaceManyDetail.setQualitieScore(resFaceData.getQualitieScore());
            studentFaceManyDetail.setRemark("");
            iStudentFaceManyDetailService.updateById(studentFaceManyDetail);

            QueryWrapper<StudentFaceMany> studentFaceManyQueryWrapper = new QueryWrapper<>();
            studentFaceManyQueryWrapper.eq("ali_account",studentFaceManyDetail.getAliAccount());
            studentFaceManyQueryWrapper.eq("student_id",studentFaceManyDetail.getStudentId());
            studentFaceManyQueryWrapper.eq("entity_id",resFaceData.getEntityId());
            Long count = iStudentFaceManyService.count(studentFaceManyQueryWrapper);
            //已经存在不添加，不存在就添加
            if (count>0){
                return true;
            }
            StudentFaceMany studentFaceMany = new StudentFaceMany();
            studentFaceMany.setStudentId(resFaceData.getStudentId());
            studentFaceMany.setFaceId(resFaceData.getFaceId());
            studentFaceMany.setAliAccount(studentFaceManyDetail.getAliAccount());
            studentFaceMany.setEntityId(resFaceData.getEntityId());
            studentFaceMany.setOssImage(studentFaceManyDetail.getOssImage());
            studentFaceMany.setQualitieScore(resFaceData.getQualitieScore());
            iStudentFaceManyService.save(studentFaceMany);
            return true;
        }else {
            studentFaceManyDetail.setRemark(resFaceData.getMsg());
            iStudentFaceManyDetailService.updateById(studentFaceManyDetail);
            return false;
        }
    }
}
